<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="学号" prop="sscStudentId">
        <el-input
          v-model="queryParams.sscStudentId"
          placeholder="请输入学号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="姓名" prop="sscName">
        <el-input
          v-model="queryParams.sscName"
          placeholder="请输入姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="年级" prop="sscGrade">
        <el-input
          v-model="queryParams.sscGrade"
          placeholder="请输入年级"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="专业" prop="sscSpecialized">
        <el-select
          v-model="queryParams.sscSpecialized"
          placeholder="请选择专业"
          clearable
        >
          <el-option
            v-for="dict in dict.type.major"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="班级" prop="sscClass">
        <el-input
          v-model="queryParams.sscClass"
          placeholder="请输入班级"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="不及格课程名" prop="sscFailedCourse">
        <el-input
          v-model="queryParams.sscFailedCourse"
          placeholder="请输入不及格课程名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="课程号" prop="sscCourseId">
        <el-input
          v-model="queryParams.sscCourseId"
          placeholder="请输入课程号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="开课学期" prop="sscCommencementSemesters">
        <el-input
          v-model="queryParams.sscCommencementSemesters"
          placeholder="请输入开课学期"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="开课院系" prop="sscFacultiesDepartments">
        <el-input
          v-model="queryParams.sscFacultiesDepartments"
          placeholder="请输入开课院系"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="任课教师" prop="sscClassroomTeacher">
        <el-input
          v-model="queryParams.sscClassroomTeacher"
          placeholder="请输入任课教师"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="学分" prop="sscCredits">
        <el-input
          v-model="queryParams.sscCredits"
          placeholder="请输入学分"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="课程成绩" prop="sscCourseGrades">
        <el-input
          v-model="queryParams.sscCourseGrades"
          placeholder="请输入课程成绩"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="四级成绩" prop="sscCetFourGrades">
        <el-input
          v-model="queryParams.sscCetFourGrades"
          placeholder="请输入四级成绩"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="六级成绩" prop="sscCetSixGrades">
        <el-input
          v-model="queryParams.sscCetSixGrades"
          placeholder="请输入六级成绩"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否违纪" prop="sscDisciplinary">
        <el-input
          v-model="queryParams.sscDisciplinary"
          placeholder="请输入是否违纪"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="第一学期专业智育排名" prop="sscRankFirstSie">
        <el-input
          v-model="queryParams.sscRankFirstSie"
          placeholder="请输入第一学期专业智育排名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="第二学期专业智育排名" prop="sscRankSecondSie">
        <el-input
          v-model="queryParams.sscRankSecondSie"
          placeholder="请输入第二学期专业智育排名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="第三学期专业智育排名" prop="sscRankThirdSie">
        <el-input
          v-model="queryParams.sscRankThirdSie"
          placeholder="请输入第三学期专业智育排名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="第四学期专业智育排名" prop="sscRankFourthSie">
        <el-input
          v-model="queryParams.sscRankFourthSie"
          placeholder="请输入第四学期专业智育排名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="第五学期专业智育排名" prop="sscRankFifthSie">
        <el-input
          v-model="queryParams.sscRankFifthSie"
          placeholder="请输入第五学期专业智育排名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="第六学期专业智育排名" prop="sscRankSixthSie">
        <el-input
          v-model="queryParams.sscRankSixthSie"
          placeholder="请输入第六学期专业智育排名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审核状态" prop="auditStatus">
        <el-select
          v-model="queryParams.auditStatus"
          placeholder="请选择审核状态"
          clearable
        >
          <el-option
            v-for="dict in dict.type.audit_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['xgssc:xgssc:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['xgssc:xgssc:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['xgssc:xgssc:remove']"
          >删除</el-button
        >
      </el-col>
      <!-- 审核   -->
      <el-col :span="1.5">
        <el-button
          type="info"
          icon="el-icon-check"
          size="mini"
          @click="openAuditDialog"
          :disabled="multiple"
          v-hasPermi="['system:check']"
          plain
          >审核</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['xgssc:xgssc:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <!-- 确保visible是通过sync双向绑定 -->
    <audit-dialog
      :visible.sync="auditDialogVisible"
      table_name="xg_study_style_construction"
      id_name="ssc_id"
      ref="audit"
      @submit="handleAuditSubmit"
    />

    <el-table
      v-loading="loading"
      :data="xgsscList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="sscId" />
      <el-table-column label="学号" align="center" prop="sscStudentId" />
      <el-table-column label="姓名" align="center" prop="sscName" />
      <el-table-column label="年级" align="center" prop="sscGrade" />
      <el-table-column label="专业" align="center" prop="sscSpecialized">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.major"
            :value="scope.row.sscSpecialized"
          />
        </template>
      </el-table-column>
      <el-table-column label="班级" align="center" prop="sscClass" />
      <el-table-column
        label="不及格课程名"
        align="center"
        prop="sscFailedCourse"
      />
      <el-table-column label="课程号" align="center" prop="sscCourseId" />
      <el-table-column
        label="开课学期"
        align="center"
        prop="sscCommencementSemesters"
      />
      <el-table-column
        label="开课院系"
        align="center"
        prop="sscFacultiesDepartments"
      />
      <el-table-column
        label="任课教师"
        align="center"
        prop="sscClassroomTeacher"
      />
      <el-table-column label="学分" align="center" prop="sscCredits" />
      <el-table-column label="课程成绩" align="center" prop="sscCourseGrades" />
      <el-table-column
        label="四级成绩"
        align="center"
        prop="sscCetFourGrades"
      />
      <el-table-column label="六级成绩" align="center" prop="sscCetSixGrades" />
      <el-table-column label="是否违纪" align="center" prop="sscDisciplinary" />
      <el-table-column
        label="第一学期专业智育排名"
        align="center"
        prop="sscRankFirstSie"
      />
      <el-table-column
        label="第二学期专业智育排名"
        align="center"
        prop="sscRankSecondSie"
      />
      <el-table-column
        label="第三学期专业智育排名"
        align="center"
        prop="sscRankThirdSie"
      />
      <el-table-column
        label="第四学期专业智育排名"
        align="center"
        prop="sscRankFourthSie"
      />
      <el-table-column
        label="第五学期专业智育排名"
        align="center"
        prop="sscRankFifthSie"
      />
      <el-table-column
        label="第六学期专业智育排名"
        align="center"
        prop="sscRankSixthSie"
      />
      <el-table-column label="审核状态" align="center" prop="auditStatus">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.audit_status"
            :value="scope.row.auditStatus"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="未通过原因"
        align="center"
        prop="rejectionReason"
      />
      <el-table-column label="用户ID" align="center" prop="userId" />
      <el-table-column label="部门id" align="center" prop="deptId" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['xgssc:xgssc:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['xgssc:xgssc:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改学风建设对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="学号" prop="sscStudentId">
          <el-input v-model="form.sscStudentId" placeholder="请输入学号" />
        </el-form-item>
        <el-form-item label="姓名" prop="sscName">
          <el-input v-model="form.sscName" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="年级" prop="sscGrade">
          <el-input v-model="form.sscGrade" placeholder="请输入年级" />
        </el-form-item>
        <el-form-item label="专业" prop="sscSpecialized">
          <el-select v-model="form.sscSpecialized" placeholder="请选择专业">
            <el-option
              v-for="dict in dict.type.major"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="班级" prop="sscClass">
          <el-input v-model="form.sscClass" placeholder="请输入班级" />
        </el-form-item>
        <el-form-item label="不及格课程名" prop="sscFailedCourse">
          <el-input
            v-model="form.sscFailedCourse"
            placeholder="请输入不及格课程名"
          />
        </el-form-item>
        <el-form-item label="课程号" prop="sscCourseId">
          <el-input v-model="form.sscCourseId" placeholder="请输入课程号" />
        </el-form-item>
        <el-form-item label="开课学期" prop="sscCommencementSemesters">
          <el-input
            v-model="form.sscCommencementSemesters"
            placeholder="请输入开课学期"
          />
        </el-form-item>
        <el-form-item label="开课院系" prop="sscFacultiesDepartments">
          <el-input
            v-model="form.sscFacultiesDepartments"
            placeholder="请输入开课院系"
          />
        </el-form-item>
        <el-form-item label="任课教师" prop="sscClassroomTeacher">
          <el-input
            v-model="form.sscClassroomTeacher"
            placeholder="请输入任课教师"
          />
        </el-form-item>
        <el-form-item label="学分" prop="sscCredits">
          <el-input v-model="form.sscCredits" placeholder="请输入学分" />
        </el-form-item>
        <el-form-item label="课程成绩" prop="sscCourseGrades">
          <el-input
            v-model="form.sscCourseGrades"
            placeholder="请输入课程成绩"
          />
        </el-form-item>
        <el-form-item label="四级成绩" prop="sscCetFourGrades">
          <el-input
            v-model="form.sscCetFourGrades"
            placeholder="请输入四级成绩"
          />
        </el-form-item>
        <el-form-item label="六级成绩" prop="sscCetSixGrades">
          <el-input
            v-model="form.sscCetSixGrades"
            placeholder="请输入六级成绩"
          />
        </el-form-item>
        <el-form-item label="是否违纪" prop="sscDisciplinary">
          <el-input
            v-model="form.sscDisciplinary"
            placeholder="请输入是否违纪"
          />
        </el-form-item>
        <el-form-item label="第一学期专业智育排名" prop="sscRankFirstSie">
          <el-input
            v-model="form.sscRankFirstSie"
            placeholder="请输入第一学期专业智育排名"
          />
        </el-form-item>
        <el-form-item label="第二学期专业智育排名" prop="sscRankSecondSie">
          <el-input
            v-model="form.sscRankSecondSie"
            placeholder="请输入第二学期专业智育排名"
          />
        </el-form-item>
        <el-form-item label="第三学期专业智育排名" prop="sscRankThirdSie">
          <el-input
            v-model="form.sscRankThirdSie"
            placeholder="请输入第三学期专业智育排名"
          />
        </el-form-item>
        <el-form-item label="第四学期专业智育排名" prop="sscRankFourthSie">
          <el-input
            v-model="form.sscRankFourthSie"
            placeholder="请输入第四学期专业智育排名"
          />
        </el-form-item>
        <el-form-item label="第五学期专业智育排名" prop="sscRankFifthSie">
          <el-input
            v-model="form.sscRankFifthSie"
            placeholder="请输入第五学期专业智育排名"
          />
        </el-form-item>
        <el-form-item label="第六学期专业智育排名" prop="sscRankSixthSie">
          <el-input
            v-model="form.sscRankSixthSie"
            placeholder="请输入第六学期专业智育排名"
          />
        </el-form-item>
        <el-form-item label="审核状态" prop="auditStatus">
          <el-select v-model="form.auditStatus" placeholder="请选择审核状态">
            <el-option
              v-for="dict in dict.type.audit_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="未通过原因" prop="rejectionReason">
          <el-input
            v-model="form.rejectionReason"
            placeholder="请输入未通过原因"
          />
        </el-form-item>
        <el-form-item label="用户ID" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户ID" />
        </el-form-item>
        <el-form-item label="部门id" prop="deptId">
          <el-input v-model="form.deptId" placeholder="请输入部门id" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listXgssc,
  getXgssc,
  delXgssc,
  addXgssc,
  updateXgssc
} from '@/api/xgssc/xgssc'
import AuditDialog from '@/components/AuditDialog/AuditDialog.vue' // 引入审核子组件

export default {
  name: 'Xgssc',
  dicts: ['major', 'audit_status'],
  components: { AuditDialog },
  data () {
    return {
      // 控制审核对话框的显示
      auditDialogVisible: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 学风建设表格数据
      xgsscList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        sscStudentId: null,
        sscName: null,
        sscGrade: null,
        sscSpecialized: null,
        sscClass: null,
        sscFailedCourse: null,
        sscCourseId: null,
        sscCommencementSemesters: null,
        sscFacultiesDepartments: null,
        sscClassroomTeacher: null,
        sscCredits: null,
        sscCourseGrades: null,
        sscCetFourGrades: null,
        sscCetSixGrades: null,
        sscDisciplinary: null,
        sscRankFirstSie: null,
        sscRankSecondSie: null,
        sscRankThirdSie: null,
        sscRankFourthSie: null,
        sscRankFifthSie: null,
        sscRankSixthSie: null,
        auditStatus: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        sscName: [
          {
            required: true,
            message: '请输入姓名',
            trigger: 'blur'
          },
          {
            pattern: /^[\u4e00-\u9fa5]{2,20}$/,
            message: '姓名只能包含中文字符',
            trigger: 'blur'
          }
        ],
        sscGrade: [
          {
            required: true,
            message: '请输入年级',
            trigger: 'blur'
          },
          {
            pattern: /^(大[一二三四])$/,
            message: '年级只能是大一、大二、大三、大四中的一个',
            trigger: 'blur'
          }
        ],
        sscSpecialized: [
          {
            required: true,
            message: '请选择专业',
            trigger: 'blur'
          }
        ],
        sscClass: [
          {
            required: true,
            message: '请输入班级',
            trigger: 'blur'
          },
          {
            pattern: /^[\u4e00-\u9fa5]{2}\d{4}$/,
            message: '班级格式为两个中文字符+四个数字',
            trigger: 'blur'
          }
        ],
        sscCommencementSemesters: [
          {
            required: true,
            message: '请输入开课学期',
            trigger: 'blur'
          },
          {
            pattern: /^\d{4}-\d{4}$/,
            message: '开课学期格式应为 YYYY-YYYY',
            trigger: 'blur'
          }
        ],
        sscFacultiesDepartments: [
          {
            required: true,
            message: '请输入院系名称',
            trigger: 'blur'
          }
        ],
        sscClassroomTeacher: [
          {
            required: true,
            message: '请输入授课教师',
            trigger: 'blur'
          }
        ],
        sscCredits: [
          {
            required: true,
            message: '请输入学分',
            trigger: 'blur'
          },
          {
            pattern: /^[0-9]+(\.[0-9]{1,2})?$/,
            message: '学分必须是数字，最多保留两位小数',
            trigger: 'blur'
          }
        ],
        sscCourseGrades: [
          {
            required: true,
            message: '请输入课程成绩',
            trigger: 'blur'
          },
          {
            pattern: /^(\d{1,2}|100)$/,
            message: '成绩应为0-100之间的数字',
            trigger: 'blur'
          }
        ],
        userId: [
          { required: true, message: '用户ID不能为空', trigger: 'blur' }
        ],
        deptId: [
          { required: true, message: '部门id不能为空', trigger: 'blur' }
        ],
        updateTime: [
          { required: true, message: '更新时间不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created () {
    this.getList()
  },
  methods: {
    // 打开审核对话框
    openAuditDialog () {
      if (this.ids != null) {
        this.auditDialogVisible = true // 显示审核对话框
        this.$refs['audit'].setData(this.ids)
      } else this.$message.error('请选择要审核的信息')
    },
    // 提交审核数据
    async handleAuditSubmit () {
      this.auditDialogVisible = false // 关闭审核对话框
      await this.getList() // 刷新数据列表
    },
    /** 查询学风建设列表 */
    getList () {
      this.loading = true
      listXgssc(this.queryParams).then(response => {
        this.xgsscList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel () {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset () {
      this.form = {
        sscId: null,
        sscStudentId: null,
        sscName: null,
        sscGrade: null,
        sscSpecialized: null,
        sscClass: null,
        sscFailedCourse: null,
        sscCourseId: null,
        sscCommencementSemesters: null,
        sscFacultiesDepartments: null,
        sscClassroomTeacher: null,
        sscCredits: null,
        sscCourseGrades: null,
        sscCetFourGrades: null,
        sscCetSixGrades: null,
        sscDisciplinary: null,
        sscRankFirstSie: null,
        sscRankSecondSie: null,
        sscRankThirdSie: null,
        sscRankFourthSie: null,
        sscRankFifthSie: null,
        sscRankSixthSie: null,
        auditStatus: null,
        rejectionReason: null,
        userId: null,
        deptId: null,
        updateTime: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange (selection) {
      this.ids = selection.map(item => item.sscId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd () {
      this.reset()
      this.open = true
      this.title = '添加学风建设'
    },
    /** 修改按钮操作 */
    handleUpdate (row) {
      this.reset()
      const sscId = row.sscId || this.ids
      getXgssc(sscId).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改学风建设'
      })
    },
    /** 提交按钮 */
    submitForm () {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.sscId != null) {
            updateXgssc(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addXgssc(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete (row) {
      const sscIds = row.sscId || this.ids
      this.$modal
        .confirm('是否确认删除学风建设编号为"' + sscIds + '"的数据项？')
        .then(function () {
          return delXgssc(sscIds)
        })
        .then(() => {
          this.getList()
          this.$modal.msgSuccess('删除成功')
        })
        .catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport () {
      this.download(
        'xgssc/xgssc/export',
        {
          ...this.queryParams
        },
        `xgssc_${new Date().getTime()}.xlsx`
      )
    }
  }
}
</script>
